import 'dart:io';

import 'package:flutter/material.dart';
import 'package:hive_flutter/adapters.dart';
import 'package:test2/base/base_page.dart';
import 'package:test2/model/game.dart';
import 'package:test2/util/PicUtil.dart';
import 'package:test2/util/bus.dart';
import 'package:test2/util/widget.dart';
import 'package:test2/view/game_add.dart';

class ItemGame extends StatefulWidget {
  const ItemGame({super.key, required this.game});

  final Game game;

  @override
  State<ItemGame> createState() => _ItemGameState();
}

class _ItemGameState extends BaseState<ItemGame> {
  @override
  Widget build(BuildContext context) {
    return BorderContainer(
      radius: 12,
      height: 100,
      bgColor: Colors.white,
      child: Row(
        spacing: 12,
        children: [
          SizedBox(
            width: 120,
            height: double.maxFinite,
            child: Image.file(
              File(PicUtil.picRootPath + widget.game.pic),
              fit: BoxFit.cover,
            ),
          ),
          Expanded(child: Text(widget.game.name)),

          Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              IconButton(
                onPressed: () {
                  showDialog(
                    context: context,
                    builder: (_) {
                      return AlertDialog(
                        title: Text("确认删除 ${widget.game.name}?"),
                        actions: <Widget>[
                          TextButton(
                            child: Text("取消"),
                            onPressed: () {
                              Navigator.of(context).pop(false);
                            },
                          ),
                          TextButton(
                            child: Text(
                              "确认",
                              style: TextStyle(color: Colors.grey),
                            ),
                            onPressed: () async {
                              await widget.game.deleteSelf();
                              bus.emit(EventType.game);
                              Navigator.of(context).pop(true);
                            },
                          ),
                        ],
                      );
                    },
                  );
                },
                icon: Icon(Icons.delete),
              ),
              Spacer(),
              Row(
                children: [
                  IconButton(
                    onPressed: () {
                      startPage(GameAdd(game: widget.game));
                    },
                    icon: Icon(Icons.edit),
                  ),
                  IconButton(
                    onPressed: () {
                      widget.game.top = !widget.game.top;
                      Hive.box<Game>("game").put(widget.game.key, widget.game);
                      bus.emit(EventType.game);
                    },
                    icon: Icon(
                      widget.game.top
                          ? Icons.push_pin
                          : Icons.push_pin_outlined,
                    ),
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
}
